package com.ruoyi.web.controller.base;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.base.BaseBom;
import com.ruoyi.system.service.IBaseBomService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 物料清单Controller
 *
 * @author ruoyi
 * @date 2023-02-07
 */
@Api(tags = "物料清单api")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/base/bom" )
public class BaseBomController extends BaseController {

    private final IBaseBomService iBaseBomService;

    /**
     * 查询物料清单列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BaseBom baseBom)
    {
        startPage();
        LambdaQueryWrapper<BaseBom> lqw = new LambdaQueryWrapper<BaseBom>();
        if (StringUtils.isNotBlank(baseBom.getBomName())){
            lqw.like(BaseBom::getBomName ,baseBom.getBomName());
        }
        if (StringUtils.isNotBlank(baseBom.getBomCode())){
            lqw.eq(BaseBom::getBomCode ,baseBom.getBomCode());
        }
        if (baseBom.getBomStatus() != null){
            lqw.eq(BaseBom::getBomStatus ,baseBom.getBomStatus());
        }
        if (StringUtils.isNotBlank(baseBom.getBomEdition())){
            lqw.eq(BaseBom::getBomEdition ,baseBom.getBomEdition());
        }
        List<BaseBom> list = iBaseBomService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出物料清单列表
     */
    @Log(title = "物料清单" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(BaseBom baseBom) {
        LambdaQueryWrapper<BaseBom> lqw = new LambdaQueryWrapper<BaseBom>(baseBom);
        List<BaseBom> list = iBaseBomService.list(lqw);
        ExcelUtil<BaseBom> util = new ExcelUtil<BaseBom>(BaseBom. class);
        return util.exportExcel(list, "bom" );
    }

    /**
     * 获取物料清单详细信息
     */
    @GetMapping(value = "/{bomId}" )
    public AjaxResult getInfo(@PathVariable("bomId" ) Long bomId) {
        return AjaxResult.success(iBaseBomService.getById(bomId));
    }

    /**
     * 新增物料清单
     */
    @Log(title = "物料清单" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseBom baseBom) {
        baseBom.setCreateBy(SecurityUtils.getUsername());
        baseBom.setCreateTime(new Date());
        return toAjax(iBaseBomService.save(baseBom) ? 1 : 0);
    }

    /**
     * 修改物料清单
     */
    @Log(title = "物料清单" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseBom baseBom) {
        baseBom.setUpdateBy(SecurityUtils.getUsername());
        baseBom.setUpdateTime(new Date());
        return toAjax(iBaseBomService.updateById(baseBom) ? 1 : 0);
    }

    /**
     * 删除物料清单
     */
    @Log(title = "物料清单" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{bomIds}" )
    public AjaxResult remove(@PathVariable Long[] bomIds) {
        return toAjax(iBaseBomService.removeByIds(Arrays.asList(bomIds)) ? 1 : 0);
    }
}
